<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "goods_package".
 *
 * @property int $id
 * @property string $goods_no 商品编号
 * @property string $name 套餐名称
 * @property string $price 套餐价格
 * @property string $install_price 每月安装维护价格
 * @property int $buy_able_time 可售起点时间
 * @property int $nums 可售起点时间
 * @property int $addtime 添加时间
 */
class GoodsPackage extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'goods_package';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['goods_no', 'name', 'addtime'], 'required'],
            [['goods_no', 'addtime'], 'integer'],
            [['price'], 'number'],
            [['name'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'goods_no' => '商品编号',
            'name' => '套餐名称',
            'price' => '套餐价格',
            'buy_able_time' => '可售起点时间',
            'addtime' => '添加时间',
        ];
    }
    
    public static function getAllPackageByGoodsno($goodsno,$first_cate=0){
        $sql = "select id,name,price,buy_able_time,nums from goods_package where goods_no=$goodsno";
        $command = \Yii::$app->db->createCommand($sql);
        $data = $command->queryAll();
        $array = [];
        foreach($data as $key=>$value){
            if($first_cate == 1){
                array_push($array,$value['name'].' '.$value['price'].' '.date('Y-m-s',$value['buy_able_time']));
            }else{
                array_push($array,$value['name'].' '.$value['price'].' '.$value['nums']);
            }
            
        }
        $result = implode("\n", $array);
        return $result;
    }
    
    public static function getPackageByGoodsno($goodsno){
        $sql = "select id,name,price,buy_able_time from goods_package where goods_no=$goodsno";
        $command = \Yii::$app->db->createCommand($sql);
        return $command->queryAll();
    }
}
